<?php

class UserController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl',
			'postOnly + delete',
		);
	}

	public function accessRules()
	{
		return array(
			array('allow',
				'actions'=>array('loginForm'),
				'users'=>array('*'),
			),
			array('allow',
				'actions'=>array('logout','changeInfo','changePassword'),
				'users'=>array('@'),
			),
			array('deny',
				'users'=>array('*'),
			),
		);
    }

    public function actionLogout() {
        Yii::app()->user->logout();
        if(isset($_GET['returnUrl'])){
            $this->redirect($_GET['returnUrl']);
        } else {
            $this->redirect(array('loginForm'));
        }
    }

    public function actionLoginForm() {
        $model = new LoginForm();
        $this->performAjaxValidation($model);
        if(isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            if($model->validate() && $model->login()) {
                if(Yii::app()->user->returnUrl == null) {
                    $this->redirect(Setting::value('site_url'));
                } else {
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
        }
        if( !Yii::app()->user->isGuest) {
            if(Yii::app()->user->returnUrl == null) {
                $this->redirect(Setting::getSetting('site_url'));
            } else {
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        $this->renderPartial('login', array('model' => $model));
    }

    public function actionChangePassword() {
        $model = $this->user;
        $this->performAjaxValidation($model);
        if(isset($_POST['User'])) {
            $model->attributes = $_POST['User'];
            if($model->validate() && $model->hashPassword($model->oldPassword) == $model->password) {
                $model->password = $model->hashPassword($model->newPassword);
                if($model->save()) {
                    Yii::app()->user->setFlash('success', "Thay đổi mật khẩu thành công!");
                    $this->redirect(array('changePassword'));
                }
            } else {
                $model->addError('oldPassword', 'Mật khẩu cũ không khớp!');
            }
        }
        $this->render('password', array('model' => $model));
    }

    public function actionChangeInfo() {
        $model = $this->user;
        $this->performAjaxValidation($model);
        if(isset($_POST['User'])) {
            $model->attributes = $_POST['User'];
            if($model->save()) {
                Yii::app()->user->setFlash('success', "Thay đổi thông tin thành công!");
                $this->redirect(array('changeInfo'));
            }
        }
        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function loadModel($id)
	{
		$model=User::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}